options(scipen=999)

args = commandArgs(trailingOnly=TRUE)

# args = c("traits/gvar-3/qtl-10000", "20", "0", "data/10000/3/20")


inputFolder = args[1]
numberOfQtl = as.numeric(args[2])
trainingSize = args[3]
outputFolder = args[4]



# I need to get qtl and phenotypes

qtl_out = list()
pheno_out = list()

for(trait in 1:numberOfQtl){
    traitFolder = paste0(inputFolder, "/", trait, "/")

    qtl = read.table(paste0(traitFolder, "qtl.", trainingSize))
    params = read.table(paste0(traitFolder, "params.", trainingSize))
    pheno = read.table(paste0(traitFolder, "pheno.txt"))

    # Note: Not including variance.
    phenotype_corrected_for_mean_and_variance = (pheno - params[1,1]) # /params[2,1] 
    ids = pheno[,1]

    qtl_out[[trait]] = qtl
    pheno_out[[trait]] = phenotype_corrected_for_mean_and_variance[,2]
}

qtl_merged = t(do.call(cbind, qtl_out))
pheno_merged = do.call(cbind, pheno_out)
pheno_merged = cbind(ids, pheno_merged)

write.table(qtl_merged, paste0(outputFolder, "/qtl.txt"), row.names=F, col.names=F, quote=F)
write.table(pheno_merged, paste0(outputFolder, "/phenotypes.children"), row.names=F, col.names=F, quote=F)





